<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>短信分组添加</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../../layui/css/layui.css" rel="stylesheet" type="text/css">
<link href="../../css/style.css" rel="stylesheet" type="text/css">
<script src="../../layui/layui.js"></script>
<script src="../../js/jquery-1.8.3.js"></script>
<script src="../../js/util.js"></script>
<script type="text/javascript">
        layui.use(['layer', 'form', 'jquery', 'laydate','upload','element'], function () {
            var layer = layui.layer;
            var form = layui.form;
            var $ = layui.$;
            var laydate = layui.laydate;
            var upload = layui.upload;
            var importNum = "";//批量导入号码
            var duty="";//操作员级别
            var operatorId="";//操作员编号

            laydate.render({
                elem: '#enterTime'
                , type: 'datetime'
                , value: new Date()
                , btns: ['confirm']
            });
            laydate.render({
	             elem: '#ComplaintTime'
	             , type: 'datetime'
	             ,max:maxDate()
	        });
	        
	        function maxDate(){
			    var now = new Date();
			    return now.getFullYear()+"-" + (now.getMonth()+1) + "-" + now.getDate()+" "+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds();
			}
			
			//根据城市ID查询区县
            function selectCountyId(cityId) {
                var data;
                var url = '/util/findCountyByCityId?cityId=' + cityId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }

			//监听地市
			form.on('select(groupCityId)', function (data) {
				var data2 = selectCountyId(data.value);
				appendGroupCounty(data2);
			});



			//根据城市ID或区县ID和项目简称查询项目
			function selectMgrInfo(countyId,mgrName) {
				var data;
				var url = '/util/findByMgrName?countyId=' + countyId +'&mgrName='+mgrName+'&groupId=1';
				$.ajax({
							type: 'post',
							async: false,
							url: url,
							success: function (json) {
								data = json.data;
							}, error: function () {
								layer.msg('系统异常，请联系管理员处理!');
							}
						}
				);
				return data;
			}
			//监听项目
			form.on('select(countyId)', function (data) {
					console.info(data.value);
					var mgrName = $("#mgrName").val();
					var cityId = $("#cityId").val();
					if(data.value==null||data.value==''){
						var data1 = selectMgrInfo(cityId,mgrName);
						appendMgr(data1);
					}else{
						console.info()
						var data1 = selectMgrInfo(data.value,mgrName);
						appendMgr(data1);
					}
			});

			//监听区县
			form.on('select(cityId)', function (data) {
				var data2 = selectCountyId(data.value);
				appendCounty(data2);
				var mgrName = $("#mgrName").val();
				var data1 = selectMgrInfo(data.value,mgrName);
				appendMgr(data1);
			});
			//动态拼接区县
			function appendCounty(data){
				if (data.length != null) {
					var options = '<option value="">全部</option>';
					for (var i = 0; i < data.length; i++) {
						options += '<option value="' + data[i].countyId + '">' + data[i].countyName + '</option>';
					}
					$('#countyId').html(options);
				} else {
					$('#countyId').html('该地市下没有区县');
				}
				form.render();
			}

			//动态拼接区县
			function appendGroupCounty(data){
				if (data.length != null) {
					var options = '<option value="">全部</option>';
					for (var i = 0; i < data.length; i++) {
						options += '<option value="' + data[i].countyId + '">' + data[i].countyName + '</option>';
					}
					$('#groupCountyId').html(options);
				} else {
					$('#groupCountyId').html('该地市下没有区县');
				}
				form.render();
			}
            //动态拼接项目
            function appendMgr(data) {
				console.info("112112");
				if(JSON.stringify(data) != "{}"){
					if (data.length >0) {
						console.info("111");
						var checkboxs = '';
						for (var i = 0; i < data.length; i++) {
							checkboxs += '<input type="checkbox" value="' + data[i].id + '" name="mgrId"  title="' + data[i].name + '" />';
						}
						$('#checkbox').html(checkboxs);
					}else {
						console.info("222");
						var checkboxs = '<span>该区域下没有项目</span>';
						$('#checkbox').html(checkboxs);
					}

				} else {
					console.info("333");
					var checkboxs = '<span>该区域下没有项目</span>';
					$('#checkbox').html(checkboxs);
				}
				form.render();
            }

			//判断项目名称是否重复
			function judgeGroupName(groupName){
				var count;
				var url = '/smsMgrGroup/judgeGroupNameRepeat';
				$.ajax({
							url: url,
							type: 'post',
							async: false,
							data: {groupName:groupName},
							success: function (json) {
								count = json.data;
							}, error: function () {
								layer.msg('系统异常，请联系管理员处理!');
							}
						}
				);
				return count;
			}
            $('#submit').on('click', function () {
                var cityId = $("#groupCityId").val();
                var countyId = $("#groupCountyId").val();
				var mgrCityId = $("#cityId").val();
				var mgrCountyId = $("#countyId").val();
                var groupName = $('#groupName').val();
                var groupDesc = $('#groupDesc').val();
                var saveDate = $("input[name='saveDate']:checked").val();
                var mgrId = "";
				var mgrCount=0;//关联项目数量
                $("input:checkbox[name='mgrId']:checked").each(function () {
					mgrCount++;
                    mgrId += $(this).val()+"+";
                });
                if (mgrId!=null&&mgrId!='') {
                	mgrId=mgrId.substring(0,mgrId.length-1);
                }
                
                var param = {
                    cityId: cityId,
                    countyId: countyId,
					mgrCityId: mgrCityId,
					mgrCountyId: mgrCountyId,
					groupName: groupName,
					groupDesc: groupDesc,
					relMgrString: mgrId,
					saveDate: saveDate,
                    operatorId: operatorId
                };
                var jsonParams=JSON.stringify(param);
                console.info(jsonParams);
                var url = '/smsMgrGroup/createGroupInfo';
                layer.confirm('确认分组信息无误吗？是否需要再次查看', {
						btn : [ '不需要', '需要' ] //按钮
					}, function() {
						$.ajax({
	                    type: 'post',
	                    data: jsonParams,
						async : false,
	                    url: url,
						contentType:"application/json;charset=UTF-8",
	                    beforeSend: function () {
							if (isNull(cityId)) {
								layer.msg("请先选择分组地市");
								return false;
							}

							if (isNull(groupName)) {
								layer.msg("分组名称不能为空");
								return false;
							}

							if (groupName.length>10) {
								layer.msg("分组名称长度不可超过10");
								return false;
							}

							if (groupDesc.length>100) {
								layer.msg("分组描述长度不可超过100");
								return false;
							}

	                    	if(judgeGroupName(groupName)>0){
								layer.msg("分组名重复，请修改");
								return false;
							}
							if(mgrCount>20){
								layer.msg("一个分组最多关联20个项目");
								return false;
							}

	                    },
	                    success: function (json) {
	                        if (json.code == "0") {
		                        layer.confirm('添加成功', {
				                    btn: ['确定'] //按钮
				                }, function () {
				                    location.href="smsGroupManage.html";
				                });
	                        }else {
	                            layer.msg('添加失败，请重试');
	                        }
	                    },
	                    complete: function () {

	                    },
	                    error: function (data) {

	                    }
	                });
				});
            });

			$('#mgrName').bind('input propertychange', function(){
				var cityId = $("#cityId").val();
				var countyId = $("#countyId").val();
				if(countyId!=null&&countyId!=''){
					var data1 = selectMgrInfo(countyId,$(this).val());
					appendMgr(data1);
				}else{
					var data1 = selectMgrInfo(cityId,$(this).val());
					appendMgr(data1);
				}
			});
            
            //身份识别
            $(document).ready(function () {
                $.ajax({
                    type: 'post',
                    async: false,
                    url: '/util/findUser',
                    success: function (json) {
                    	duty=json.duty;
    					operatorId = json.userId;
    					console.info(JSON.stringify(json));
						var mgrName = $("#mgrName").val();
    					
    					if (json.duty == 1) {//地市

							$("#groupCityId").val(json.cityId);
							$("#groupCityId").attr("disabled", true);
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            var data2 = selectCountyId(json.cityId);
                            appendCounty(data2);
                            var data1 = selectMgrInfo(json.cityId,mgrName);
                            appendMgr(data1);
                            form.render();
                        } else if (json.duty == 2) {//区县
							var data2 = selectCountyId(json.cityId);
							appendCounty(data2);
							var data3 = selectCountyId(json.cityId);
							appendGroupCounty(data3);
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
							$("#groupCityId").val(json.cityId);
							$("#groupCityId").attr("disabled", true);
                            $("#countyId").val(json.countyId);
                            $("#countyId").attr("disabled", true);
							$("#groupCountyId").val(json.countyId);
							$("#groupCountyId").attr("disabled", true);
							var data = selectMgrInfo(json.countyId,mgrName);
							appendMgr(data);

                            form.render();
                        }else if (json.duty == 3) {//项目
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);

                            var data = selectMgrInfo(json.countyId,mgrName);
                            appendMgr(data);
                            var data2 = selectCountyId(json.cityId);
                            appendCounty(data2);
							$("#groupCityId").val(json.cityId);
							$("#groupCityId").attr("disabled", true);
							$("#countyId").val(json.countyId);
							$("#countyId").attr("disabled", true);
							$("#mgrCountyId").val(json.countyId);
							$("#mgrCountyId").attr("disabled", true);

                            form.render();
                        }else if(json.duty == 0){
                            if(json.cityId==25){//省权限
								var checkboxs = '<span>该区域下没有项目</span>';
								$('#checkbox').html(checkboxs);
                            }else if(json.cityId!=25&&checkStr(json.countyId)==""){//地市权限
	                            $("#cityId").val(json.cityId);
	                            $("#cityId").attr("disabled", true);
	                            var data2 = selectCountyId(json.cityId);
	                            appendCounty(data2);
	                            var data1 = selectMgrInfo(json.cityId,mgrName);
	                            appendMgr(data1);
	                            form.render();
                            }else if(json.cityId!=25&&checkStr(json.countyId)!=""){//区县权限
	                            $("#cityId").val(json.cityId);
	                            $("#cityId").attr("disabled", true);
	                            var data = selectMgrInfo(json.countyId,mgrName);
	                            appendMgr(data);
	                            var data2 = selectCountyId(json.cityId);
	                            appendCounty(data2);
	                            $("#countyId").val(json.countyId);
	                            $("#countyId").attr("disabled", true);
	                            form.render();
                            }
                        } 
                    }, error: function () {
                        layer.msg('系统异常，请联系管理员处理!');
                    }
                });


                 // var cityId = $("#cityId").val();
                 // var data2 = selectCountyId(cityId);
                 // appendCounty(data2);
                 // var data1 = selectMgrInfo(cityId);
                 // appendMgr(data1);
            });
        });

    </script>
</head>
<body>
	<div class="layui-content-body">
		<div class="scroll-box">
			<div class="breadcrumb-box">
				<span class="layui-breadcrumb" lay-separator="|"> <a
					href="javascript:returnIndex2()">首页&nbsp;</a>
					<a href="smsGroupManage.html"> 短信分组管理</a> <a href="smsGroupAdd.html">
						短信分组添加</a>
				</span>
			</div>
			<div class="content-box">
				<div class="form-select-box">
					<div class="layui-form layui-form-pane">
						<div class="layui-form-item">
							<div class="layui-form-item">
								<div class="layui-inline groupCityId">
									<label class="layui-form-label">分组地市</label>
									<div class="layui-input-block" style="width:120px;">
										<select name="groupCityId" lay-filter="groupCityId" id="groupCityId">
											<option value="">请选择地市</option>
											<option value="11">苏州</option>
											<option value="12">淮安</option>
											<option value="13">宿迁</option>
											<option value="14">南京</option>
											<option value="15">连云港</option>
											<option value="16">徐州</option>
											<option value="17">常州</option>
											<option value="18">镇江</option>
											<option value="19">无锡</option>
											<option value="20">南通</option>
											<option value="21">泰州</option>
											<option value="22">盐城</option>
											<option value="23">扬州</option>
											<option value="25">省级</option>
										</select>
									</div>
								</div>
								<div class="layui-inline groupCountyId">
									<label class="layui-form-label">分组区县</label>
									<div class="layui-input-block" style="width:150px">
										<select name="groupCountyId" lay-filter="groupCountyId" id="groupCountyId">
										</select>
									</div>
								</div>
							</div>
							<div class="layui-form-item">
								<div class="layui-inline">
									<label class="layui-form-label">分组名称</label>
									<div class="layui-input-block">
										<input type="text" id="groupName" name="groupName" lay-verify="required" placeholder="请输入分组名称"
											   autocomplete="off" class="layui-input">
									</div>
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">分组描述</label>
								<div class="layui-input-block">
							<textarea placeholder="请输入分组描述" class="layui-textarea"
									  style="width:30%" id="groupDesc"></textarea>
								</div>
							</div>
							<div class="layui-form-item hiden" id="local">
							<div class="layui-block" pane="">
								<label class="layui-form-label" style="width: auto">号码保存时间</label>
								<div class="layui-input-block">
									<input type="radio" name="saveDate"value="1" title="1天" >
									<input type="radio" name="saveDate"value="3" title="3天">
									<input type="radio" name="saveDate"value="5" title="5天" >
									<input type="radio" name="saveDate" value="7" title="7天"  checked="">
									<input type="radio" name="saveDate"value="15" title="15天" >
									<input type="radio"name="saveDate" value="30" title="30天" >
									<input type="radio" name="saveDate" value="90" title="90天" >
									<input type="radio" name="saveDate" value="180" title="180天">
								</div>
							</div>
						</div>
							<div class="layui-inline cityId">
								<label class="layui-form-label">项目地市</label>
								<div class="layui-input-block" style="width:120px;">
									<select name="cityId" lay-filter="cityId" id="cityId">
										<option value="">请选择地市</option>
										<option value="11">苏州</option>
										<option value="12">淮安</option>
										<option value="13">宿迁</option>
										<option value="14">南京</option>
										<option value="15">连云港</option>
										<option value="16">徐州</option>
										<option value="17">常州</option>
										<option value="18">镇江</option>
										<option value="19">无锡</option>
										<option value="20">南通</option>
										<option value="21">泰州</option>
										<option value="22">盐城</option>
										<option value="23">扬州</option>
										<option value="25">省级</option>
									</select>
								</div>
							</div>
							<div class="layui-inline countyId">
								<label class="layui-form-label">项目区县</label>
								<div class="layui-input-block" style="width:150px">
									<select name="countyId" lay-filter="countyId" id="countyId">
									</select>
								</div>
							</div>
							<div class="layui-inline mgrName">
								<label class="layui-form-label">项目简称</label>
								<div class="layui-input-block" style="width:150px">
									<input type="text" name="mgrName" id="mgrName"
										   lay-verify="required" placeholder="请输入项目简称" autocomplete="off"
										   class="layui-input" />
								</div>
							</div>
						</div>
						<div class="layui-form-item checkbox">
							<div class="layui-inline">
								<label class="layui-form-label">关联项目</label>
								<div class="layui-input-block" id="checkbox"></div>
							</div>
						</div>
						<div class="layui-block">
							<button class="layui-btn layui-btn-normal" id="submit">提&nbsp;交</button>
							<a href="smsGroupManage.html"><button class="layui-btn" style="background-color: #1E9FFF">返回</button></a>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</body>
</html>